<template>
  <div>
    <h2>添 加 虫 害</h2>
    <div class="add">
      <div class="left">
        <el-form :label-position="labelPosition" label-width="80px">
          <el-form-item label="名称">
            <el-input v-model="pestName"></el-input>
          </el-form-item>
          <el-form-item label="繁殖">
            <el-input v-model="pestBreed"></el-input>
          </el-form-item>

          <el-form-item label="幼虫图片">
            <el-upload
              class="avatar-uploader"
              action="/api/pest/upload2"
              :http-request="testUpload"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="防治措施">
            <el-input v-model="pestControl"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div>
        <el-form :label-position="labelPosition" label-width="80px">
          <el-form-item label="寄主">
            <el-input v-model="pestHost"></el-input>
          </el-form-item>
          <el-form-item label="天敌">
            <el-input v-model="pestSentinel"></el-input>
          </el-form-item>
          <el-form-item label="成虫图片">
            <el-upload
              class="avatar-uploader"
              action="/api/pest/upload2"
              :http-request="testUpload2"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl2" :src="imageUrl2" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="主要危害">
            <el-input v-model="pestMainHarm"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-row>
      <el-button plain @click="addPest()">添加</el-button>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pestName: "",
      pestHost: "",
      pestBreed: "",
      pestSentinel: "",
      pestLarvaPic: "",
      pestAdultPic: "",
      pestControl: "",
      pestMainHarm: "",
      labelPosition: "left",
      imageUrl: "",
      imageUrl2: "",
    };
  },
  methods: {
    async testUpload(item) {
      this.pestLarvaPic = await this.ajax.uploadSubmit("/api/pest/upload2", {
        file: item.file,
      });
      //console.log(this.pestLarvaPic);
      this.imageUrl = "/api/image/" + this.pestLarvaPic;
      this.pestLarvaPic = "/image/" + this.pestLarvaPic;
      console.log(this.pestLarvaPic);
    },

    async testUpload2(item) {
      this.pestAdultPic = await this.ajax.uploadSubmit("/api/pest/upload2", {
        file: item.file,
      });
      //console.log(this.pestAdultPic);
      this.imageUrl2 = "/api/image/" + this.pestAdultPic;
      this.pestAdultPic = "/image/" + this.pestAdultPic;
      console.log(this.pestAdultPic);
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    async addPest() {
      let paramObj = {
        pestName: this.pestName,
        pestHost: this.pestHost,
        pestBreed: this.pestBreed,
        pestSentinel: this.pestSentinel,
        pestLarvaPic: this.pestLarvaPic,
        pestAdultPic: this.pestAdultPic,
        pestControl: this.pestControl,
        pestMainHarm: this.pestMainHarm,
      };
      let info = await this.ajax.getSubmit("/api/pest/add", paramObj);
      console.log(info);

      this.pestName = "";
      this.pestHost = "";
      this.pestBreed = "";
      this.pestSentinel = "";
      this.pestLarvaPic = "";
      this.pestAdultPic = "";
      this.pestControl = "";
      this.pestMainHarm = "";
    },
  },
};
</script>

<style>
.el-input__inner {
  width: 240px !important;
}
.add {
  display: flex;
}
.add .left {
  margin-right: 150px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>